package sz.szsmk.citizencard.mvp.dzsmkpresenter;

import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.functions.Action1;
import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.CheckBankCodeRes;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.model.network.dzbean.req.CheckBankCardRequset;
import sz.szsmk.citizencard.model.network.dzbean.req.CommonRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.DzsmkAgreementReq;
import sz.szsmk.citizencard.model.network.dzbean.req.GetBankNameRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.PreAuthThiedReq;
import sz.szsmk.citizencard.model.network.dzbean.req.PreAuthThirdRes;
import sz.szsmk.citizencard.model.network.dzbean.res.Bank;
import sz.szsmk.citizencard.model.network.dzbean.res.GetBankNameRes;
import sz.szsmk.citizencard.model.network.dzbean.res.PreAuthResult;
import sz.szsmk.citizencard.model.network.dzbean.res.SupportBankResponse;
import sz.szsmk.citizencard.mvp.contract.IBindCashCardContact;
import sz.szsmk.citizencard.mvp.presenter.IBasePresenter;
import sz.szsmk.citizencard.retrofitrxjava.DZSmkAppInit;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.ObjectSignatureUtil;
import sz.szsmk.citizencard.util.PubUtils;
import sz.szsmk.citizencard.widget.GlobalVariables;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/9/11 15:33
 * description:
 */
public class BindCashCardOldPresenter extends IBasePresenter
        implements IBindCashCardContact.IBindCashCardPresenter<IBindCashCardContact.IBindCashCardView> {

    private NetworkDateSource mNetworkDateSource;
    private NetworkDateSources mNetworkDate;
    GsdxNetApi gsdxNetApi;
    private ArrayList<Bank> list = new ArrayList<>();

    @Inject
    public BindCashCardOldPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSource.getInstance();
        this.mNetworkDate = NetworkDateSources.getInstance();
    }

    @Override
    public void getSupportBankList(String openId, String channelId, String token) {
        CommonRequest commonRequest1 = new CommonRequest();
        commonRequest1.setOpenId(openId);
        commonRequest1.setChannelId("000003");
        ObjectSignatureUtil.signatureRequest(commonRequest1);
        String json = new Gson().toJson(commonRequest1);
        L.e(Constant.TAG, "getSupportBankList---->" + json);
        mNetworkDateSource.getSupportBankList(commonRequest1, new Action1<SupportBankResponse>() {

            private Bank bank;

            @Override
            public void call(SupportBankResponse baseRes) {
                String toJson = new Gson().toJson(baseRes);
                L.e(Constant.TAG, "getSupportBankList---->" + toJson);
                String code = baseRes.getCode();
                if (code != null && code.equals("0000")) {
                    List<SupportBankResponse.DataBean> data = baseRes.getData();
                    if (data != null) {
                        for (int i = 0; i < data.size(); i++) {
                            SupportBankResponse.DataBean dataBean = data.get(i);
                            String json = new Gson().toJson(dataBean);
                            bank = new Gson().fromJson(json, Bank.class);
                            list.add(bank);
                        }
                    }
                    ((IBindCashCardContact.IBindCashCardView) mView).getSupportBankListSucc(data);
//                    getMvpView().onGetBanksInfo(list);
                } else {

                    PubUtils.popTipOrWarn(DaggerApplication.getIns(), baseRes.getMessage());
//                    ((IBindCashCardContact.IBindCashCardView) mView).getSupportBankListFail();
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                String message = throwable.getMessage();
                PubUtils.popTipOrWarn(DaggerApplication.getIns(), message);
//                ((IBindCashCardContact.IBindCashCardView) mView).getSupportBankListFail();
            }
        });

    }

    @Override
    public void dzsmkAgreement(String openId, final String channelId) {
        DzsmkAgreementReq dzsmkAgreementReq = new DzsmkAgreementReq();
        dzsmkAgreementReq.setChannelId(channelId);
        dzsmkAgreementReq.setOpenId(openId);
        dzsmkAgreementReq.setUserType(0);
        String json = new Gson().toJson(dzsmkAgreementReq);
        L.e(Constant.TAG, "dzsmkAgreementReq---->" + json);
        mNetworkDate.dzsmkAgreement(dzsmkAgreementReq, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                ((IBindCashCardContact.IBindCashCardView) mView).dzsmkAgreementSucc(channelId);
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                ((IBindCashCardContact.IBindCashCardView) mView).dzsmkAgreementFail(response);
            }

        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void preAuthThird(String openId, String mobile, String userId, String channelId) {
        PreAuthThiedReq preAuthThiedReq = new PreAuthThiedReq();
        PreAuthResult authResult = GlobalVariables.getInstance(DZSmkAppInit.getInstance().getContext()).getPreAuthResult();
        String openId1 = authResult.getOpenId();
        String mobile1 = authResult.getMobile();
        String userId1 = authResult.getUserId();
        preAuthThiedReq.setMobileNumber(mobile);
        preAuthThiedReq.setOpenId(openId);
        preAuthThiedReq.setUserId(userId);
        preAuthThiedReq.setPhotoUrl("");
        preAuthThiedReq.setChannelId(channelId);

        ObjectSignatureUtil.signaturePreAuthRequest(preAuthThiedReq);
        String toJson = new Gson().toJson(preAuthThiedReq);
        L.e(Constant.TAG, "preAuthThird------>" + toJson);
        mNetworkDateSource.preAuthThird(preAuthThiedReq, new Action1<PreAuthThirdRes>() {
            @Override
            public void call(PreAuthThirdRes preAuthRes) {
                PreAuthThirdRes.DataBean data = preAuthRes.getData();
                String json = new Gson().toJson(data);
                ACache.get(DaggerApplication.getIns()).put("token", preAuthRes.getData().getToken());
                PreAuthResult preAuthResult = new Gson().fromJson(json, PreAuthResult.class);
                GlobalVariables.getInstance(DZSmkAppInit.getInstance().getContext()).setPreAuthResult(DaggerApplication.getIns(), preAuthResult);
//                getSysConfig();
                ((IBindCashCardContact.IBindCashCardView) mView).preAuthThirdSucc(preAuthRes);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

                L.e(Constant.TAG, "--------------------" + throwable.getMessage());
            }
        });
    }

    @Override
    public void getBankName(String cardNo, String channelId, String openId, String token) {
        final GetBankNameRequest getBandCardNameReq = new GetBankNameRequest();
        getBandCardNameReq.setBankCardNumber(cardNo);
        getBandCardNameReq.setOpenId(openId);
        ObjectSignatureUtil.signatureRequest(getBandCardNameReq);
        String json = new Gson().toJson(getBandCardNameReq);
        L.e(Constant.TAG, "getBankName---->" + json);
        mNetworkDateSource.getBankName(getBandCardNameReq, new Action1<GetBankNameRes>() {
            @Override
            public void call(GetBankNameRes getBankNameRes) {
                String toJson = new Gson().toJson(getBankNameRes);
                L.e(Constant.TAG, "getBankName---->" + toJson);
                if (getBankNameRes.getCode().equals("0000")) {
                    ((IBindCashCardContact.IBindCashCardView) mView).getBankNameSucc(getBankNameRes);
                } else {
                    ((IBindCashCardContact.IBindCashCardView) mView).getBankNameFail(getBankNameRes);
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });

    }

    @Override
    public void checkBankNoValidation(String cardNo, String channelId, String openId, String token,String selectBankCode) {
        final CheckBankCardRequset checkBankCardRequset = new CheckBankCardRequset();
        checkBankCardRequset.setBankCardNo(cardNo);
        checkBankCardRequset.setBankbinCode(selectBankCode);
        String json = new Gson().toJson(checkBankCardRequset);
        L.e(Constant.TAG, "checkBankNoValidation---->" + json);
        mNetworkDateSource.checkBankNoValidation(checkBankCardRequset, new Action1<CheckBankCodeRes>() {
            @Override
            public void call(CheckBankCodeRes getBankNameRes) {
                String toJson = new Gson().toJson(getBankNameRes);
                L.e(Constant.TAG, "checkBankNoValidation---->" + toJson);
                if (getBankNameRes.getCode().equals("0000")) {
//                    未能识别：-1
//                            * 本行储蓄卡(包括准贷记卡)：1
//                            * 他行储蓄卡(包括准贷记卡)：2
//                            * 他行贷记卡：3
//                            * 本行贷记卡：4

                    switch (getBankNameRes.getData().getQueryBankCode()){
                        case "1":
                        case "2":
                            getBankName(cardNo, "", openId, token);
                            break;
                        default:
                            ((IBindCashCardContact.IBindCashCardView) mView).checkBankNoValidationFail(getBankNameRes.getMessage());
                            break;
                    }
                } else {
                    ((IBindCashCardContact.IBindCashCardView) mView).checkBankNoValidationFail(getBankNameRes.getMessage());
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

}
